import Serv from './addressMaintenanceServ';
var regeneratorRuntime = require("../../../../lib/js/runtime-module.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailAddress:'',
    mobile:'',
    name:'',
    prizeId:'',
    isRevise:'',
    showModal:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //detailAddress,mobile,name,isRevise
     this.setData({
       ...options
     })
  },
  chageInput:function(e){
    let name = e.currentTarget.dataset.name
    let value =value = e.detail.value
    this.setData({
      [name]:value
    })
  },
  closeModel(){
    this.setData({
      showModal:!this.data.showModal
    })
  },
  chooseLocation: function (e) {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        that.setData({
          detailAddress:res.address + res.name,
        })
      }
    })

  },
  formSubmit: function (e) {
    let {name,mobile,detailAddress} = this.data;
    if(name==""||mobile==""||detailAddress==""){
      wx.showToast({
        title: '请输入必填信息',
        icon: 'none'
      })
      return false;
    }else if(mobile.length!=11){
      wx.showToast({
        title: "手机号码不正确",
        icon: "none"
      });
      return false
    }
    else{
      if(this.data.isRevise){
        this.setData({showModal:!this.data.showModal})
      }else{
        this.addorEdit()
      }
     
    
    }
  },
  // 新增地址
  async addorEdit(){
    let param ={
      detailAddress:this.data.detailAddress,
      mobile:this.data.mobile,
      name:this.data.name,
      prizeId:this.data.prizeId
    }
    wx.showLoading()
    let {success,message} = await Serv.gameAddressServ(param)
    if(success){
      wx.showToast({
        title:'保存成功',
        icon:'success'
      })
      let pages = getCurrentPages();//当前页面
      let prevPage = pages[pages.length - 2];//上一页面
      if(prevPage.showMyPrize){
        prevPage.showMyPrize()
       
        prevPage.setData({
          showModel:false,
          showMask:false
        });
      }
      wx.navigateBack({
        delta: 1
      });
    }else{
      wx.showToast({
        title:message
      })
    }
    wx.hideLoading()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})